import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

class MyThreadPool{
    //任务队列
    private BlockingQueue<Runnable> queue=new ArrayBlockingQueue<>(1000);

    //任务添加到队列中
    public void submit(Runnable runnable) throws InterruptedException {
        //这里的拒绝策略——阻塞等待
        queue.put(runnable);
    }
    public MyThreadPool(int n){
        //创建n个线程负责执行上述队列中的任务
        for (int i = 0; i < n; i++) {
            Thread t=new Thread(()->{
                Runnable runnable= null;
                try {
                    runnable = queue.take();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                runnable.run();
            });
            t.start();
        }
    }
}

public class demo9 {
    public static void main(String[] args) throws InterruptedException {
        MyThreadPool myThreadPool=new MyThreadPool(4);
        for (int i = 0; i < 1000; i++) {
            int id=i;
            myThreadPool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("执行任务："+id);//现在捕获的不是i了，而是id(变量捕获捕获final)
                }
            });
        }
    }
}
